<?php
@session_start();

require_once '../../config/config.inc';


$errmsg=array();
$errflag=false;
$conn = mysql_connect(DB_HOST,DB_USER,DB_PASSWORD);
if(!$conn){
	echo mysql_error();
}
$db=mysql_select_db(DB_DATABASE);
if(!$db){
	mysql_error();
}
function clean($str){
	if(get_magic_quotes_gpc()){
		$str=stripcslashes($str);
	}

	return mysql_real_escape_string($str);
}
if(isset($_POST['submit'])) {
	$title=clean($_POST['title']);
	$sdate=clean($_POST['sdate']);
	$edate=clean($_POST['edate']);
	$select=clean($_POST['select']);
	$venue=clean($_POST['venue']);
	$description=clean($_POST['textarea']);
	$photo=$_FILES['photo']['name'];
	$agenda=$_FILES['file']['name'];
	$speakar=clean($_POST['speakar']);
	$partnar=clean($_POST['partnar']);
	$sponsor=clean($_POST['sponsor']);
	$organizer=clean($_POST['organizer']);
	$exihabitevent=clean($_POST['exhibition']);
	$sponsorevent=clean($_POST['sponsor']);
	if($title==''){
		$errmsg[]='Title is Missing';
		$errflag=true;
	}
	if($sdate==''){
		$errmsg[]='Start Date  is Missing';
		$errflag=true;
	}
	if($edate==''){
		$errmsg[]='End Date is Missing';
		$errflag=true;
	}


	if ($select==''){
		$errmsg[]="Please select the One Catagories ";
		$errflag=true;
	}
	if($venue==''){
		$errmsg[]="Please enter the venue";
		$errflag=true;
	}
	if($description==''){
		$errmsg[]="Briefly interduces your Event";
		$errflag=true;
	}
	if($photo==''){
		$errmsg[]="Its required that you must Upload Photo  ";
		$errflag=true;
	}
	if($errflag){
		$_SESSION['ERROR_MSG']=$errmsg;
		session_write_close();
		header("location:../addevent.php");
		exit();
	}


	$uploadfile="../../assets/uploadfile/";
	$uploadfile=$uploadfile.basename($_FILES['file']['name']);
	$ok=1;
	if(move_uploaded_file($_FILES['file']['tmp_name'],$uploadfile)){
		echo "file has been uploaded".basename($_FILES['file']['name']);
	}else{
		echo mysql_error()."file hasbeen not uploaded try agine";
	}


	$txt;$ext;
	mysql_query("SET NAMES UTF8");
	mysql_query("SET CHARACHAR_SET UTF8");
	$path="../../assets/img/eventphoto/";
	$validformat=array("jpg","png","gif","bmp","JPG","PNG","GIF","BMP");
	$name=$_FILES['photo']['name'];
	$size=$_FILES['photo']['size'];
	if(strlen($name)){
		$r=1;
		list($txt,$ext)=explode(".",$name);
		if (is_array($validformat)){
			if($size<(1024*1024)*6){
				$r=3;
				$atual_name=substr(str_replace(" ","_", $txt), 5).".".$ext;
				$temp=$_FILES['photo']['tmp_name'];
				$insert=mysql_query("INSERT INTO `event` VALUES(null,'$title','$sdate','$edate',
				'$venue','$description','$atual_name','$agenda','$speakar','$partnar','$sponsor',
				'$organizer','$exihabitevent','$sponsorevent','$select')");
				if($insert){
					$movefile=move_uploaded_file($temp , $path.$atual_name);
					if(!$movefile){
						echo mysql_error();
						echo "FAILED";
						exit();
					}
					$we=mysql_query("select * from `subscription`");
					if ($we){
						while ($row=mysql_fetch_array($we)){
							$fname=$row['fullname'];
							$to=$row['email'];
							$subject=" Thank you for subscribing events.af";
							$header="From:Technation:< info@events.af>";
							$message="Dear $fname,
Thank you for subscribing events.af, Afghanistan's events portal. 
We will be keeping you fully alert on national, regional and global 
events-- conference, workshops, tournaments, concerts. 
Please, be involved with us, and let us know if you have querries or suggestions. 
Sincerely, 
Events.af Team";
$message.="http://www.events.af/index.php";
$sentmail=mail($to, $subject, $message,$header);
							if(!$sentmail){
								echo mysql_error();

							}
								
						}}
						header("location:../addevent.php?status=suesses");
				}else{

					echo mysql_error()."not inserted dear please try agine";
				}

			}else{
				echo mysql_error()."name is not true";
			}
		}}
}else {
	echo mysql_error();
}
?>